/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller.all;

import dao.SanphamDAO;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;
import pojo.Sanpham;

/**
 *
 * @author BlueZky
 */
public class dcController extends AbstractController {

    public dcController() {
    }

    @Override
    protected ModelAndView handleRequestInternal(
            HttpServletRequest request,
            HttpServletResponse response) {
        HttpSession session = request.getSession();
        ModelAndView mv = null;
        List<Sanpham> danhSach = null;
        String cid = request.getParameter("cid");
        
        mv = new ModelAndView("All/index");
        mv.addObject("tag", "danhmuc");
        if (cid == null || cid.length() == 0 || !cid.matches("^[0-9]*$")) {
        } else {
            String page = request.getParameter("page");
            String num = request.getParameter("num");
            int soSp = 0;
            soSp = SanphamDAO.laySoSanPhamTheoLoai(Integer.parseInt(cid));

            if (page == null || page.length() == 0 || !page.matches("^[0-9]*$")
                    || num == null || num.length() == 0 || !num.matches("^[0-9]*$")) {
                danhSach = SanphamDAO.laySanPhamTheoLoai(Integer.parseInt(cid), 0, 8);
                mv.addObject("page", 0);
                mv.addObject("num", 8);
                mv.addObject("last", (soSp - 1)/8);
            } else {
                danhSach = SanphamDAO.laySanPhamTheoLoai(Integer.parseInt(cid), Integer.parseInt(page), Integer.parseInt(num));
                mv.addObject("page", Integer.parseInt(page));
                mv.addObject("num", Integer.parseInt(num));
                mv.addObject("last", (soSp - 1)/Integer.parseInt(num));
            }

            mv.addObject("cid", cid);
        }
        //danhSach = SanphamDAO.layDanhSach(12);
        //danhSach = SanphamDAO.layDanhSach(10);


        mv.addObject("danhSach", danhSach);
        return mv;
    }
}
